package com.rd.student.ruanjian.class1.duhang.Week04.New;

import java.util.Scanner;
import java.util.concurrent.locks.ReentrantLock;

public class Bank1 extends Thread{
    Account account;
    double outmoney;
    double inmoney;

    public Bank1(String name, Account account ,double outmoney, double inmoney) {
        super(name);
        this.account = account;
        this.outmoney = outmoney;
        this.inmoney = inmoney;
    }

    ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        //取钱
        lock.lock();
        if (account.balance>= outmoney){
            System.out.println("可以取钱");
            account.balance -= outmoney;
            System.out.println(getName() + "余额为：" + account.balance);
        }else {
            System.out.println("余额不足");
        }
        lock.unlock();
        //存钱
        lock.lock();
        account.balance += inmoney;
        System.out.println(getName() + "余额为：" + account.balance);
        lock.unlock();
    }

    public static void main(String[] args) {
        System.out.println("请创建新账户");
        Scanner sc = new Scanner(System.in);
        System.out.println("输入账户信息：id,密码");
        String accid01 = sc.next();
        String pass01 = sc.next();

        System.out.println("输入第二个账户信息：id,密码");
        String accid02 = sc.next();
        String pass02 = sc.next();

        Account account00 = new Account("111","11111",99999);
        Account account = new Account(accid01,pass01,0);
        Account account01 = new Account(accid02,pass02,0);

        Account[] ac = {account00,account,account01};

        boolean a = true;
      while (a){
          System.out.println("1.核对账户密码");
          System.out.println("2.核对账户信息");
          System.out.println("3.存取钱查看余额");
          System.out.println("4.退出登陆,登陆其他账户");
          System.out.println("5.退出系统");
          switch (sc.nextInt()){

              case 1:
                  //查看账户密码
                  System.out.println("账户1的密码是：" + account00.password);
                  System.out.println("账户2的密码是：" + account.password);
                  System.out.println("账户3的密码是：" + account01.password);
                  break;

              case 2:
                  //查看账户信息
                  System.out.println("账户1的信息为：" + account00);
                  System.out.println("账户2的信息为：" + account);
                  System.out.println("账户3的信息为：" + account01);
                  break;

              case 3:
                  //输入密码存取钱
                          System.out.println("输入您要存入的账户,0，1，2");
                          int i = sc.nextInt();
                          if (i == 0){
                              System.out.println("请输入密码");
                              String pa1 = sc.next();
                              if (pa1.equals(account00.password) ){
                                  System.out.println("密码正确");
                                  System.out.println("输入您要存入的金额");
                                  double in = sc.nextDouble();
                                  System.out.println("输入您要取出的金额");
                                  double out = sc.nextDouble();
                                  System.out.println("成功");
                                  new Bank1("zhangsan",ac[i],out,in).start();
                              }else System.out.println("密码错误");
                          }
                          if (i == 1){
                              System.out.println("请输入密码");
                              String pa1 = sc.next();
                              if (pa1.equals(account.password) ){
                                  System.out.println("密码正确");
                                  System.out.println("输入您要存入的金额");
                                  double in = sc.nextDouble();
                                  System.out.println("输入您要取出的金额");
                                  double out = sc.nextDouble();
                                  System.out.println("成功");
                                  new Bank1("zhangsan",ac[i],out,in).start();
                              }else System.out.println("密码错误");
                          }
                          if (i == 2){
                              System.out.println("请输入密码");
                              String pa1 = sc.next();
                              if (pa1.equals(account01.password) ){
                                  System.out.println("密码正确");
                                  System.out.println("输入您要存入的金额");
                                  double in = sc.nextDouble();
                                  System.out.println("输入您要取出的金额");
                                  double out = sc.nextDouble();
                                  System.out.println("成功");
                                  new Bank1("zhangsan",ac[i],out,in).start();
                              }else System.out.println("密码错误");
                          }
                          break;

              case 4:
                  //退出登陆

              case 5:
                  //结束运行
                  a = false;

          }
       }
    }
}

class Account{
    //创建账户类
    String accountid;
    String password;
    double balance;

    public Account() {}

    public Account(String accountid, String password, double balance) {
        this.accountid = accountid;
        this.password = password;
        this.balance = balance;
    }

    public String getAccountid() {

        return accountid;
    }

    public void setAccountid(String accountid) {

        this.accountid = accountid;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {

        this.password = password;
    }

    public double getBalance() {

        return balance;
    }

    public void setBalance(double balance) {

        this.balance = balance;
    }

    @Override
    public String toString() {
        return "账户：" +
                "账户id=" + accountid + '\'' +
                ", 账户密码=" + password + '\'' +
                ", 账户余额" + balance ;
    }
}
